package com.koalton.steward.flow.dao;

import com.koalton.steward.flow.model.FlowItem;
import com.koalton.steward.flow.model.ItemType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by koalton on 2017/7/11.
 */
@Repository
public class FlowItemDaoImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate ;

    /**
     * 通过条款类型查询条款
     * @param itemType 条款类型
     * @return 若查询不到数据则返回一个size为0的list
     */
    public List<FlowItem> list( ItemType itemType){
        StringBuilder queryBuildrStr = new StringBuilder();
        queryBuildrStr.append("SELECT ITEM_ID,ITEM_NAME,ITEM_KIND_CODE,ITEM_KIND_NAME ");
        queryBuildrStr.append("FROM FLOW_ITEM WHERE ITEM_TYPE = ? ");
        final ItemType itemTypeTemp = itemType;
        List<FlowItem> flowItems = (List<FlowItem>) jdbcTemplate.query(queryBuildrStr.toString(), new ResultSetExtractor() {
                    List<FlowItem> flowItemsInner = new ArrayList<FlowItem>();
                    @Override
                    public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
                        while(resultSet.next()){
                            FlowItem flowItem = new FlowItem();
                            flowItem.setID(resultSet.getInt("ITEM_ID"));
                            flowItem.setName(resultSet.getString("ITEM_NAME"));
                            flowItem.setKindCode(resultSet.getInt("ITEM_KIND_CODE"));
                            flowItem.setKindName(resultSet.getString("ITEM_KIND_NAME"));
                            flowItem.setType(itemTypeTemp);
                            flowItemsInner.add(flowItem);
                        }
                        return flowItemsInner;
                    }
                }, itemType.getVlaue());
        return flowItems ;
    }


    /**
     * 通过条款类型、条款种类码查询条款
     * @param itemType 条款类型
     * @param itemKindCode 条款种类码
     * @return 若查询不到值则返回的对象中值为空
     */
    public List<FlowItem> list(ItemType itemType, Integer itemKindCode){
        StringBuilder queryBuildrStr = new StringBuilder();
        queryBuildrStr.append("SELECT ITEM_ID,ITEM_NAME,ITEM_KIND_NAME ");
        queryBuildrStr.append("FROM FLOW_ITEM WHERE ITEM_TYPE = ? AND ITEM_KIND_CODE = ? ");
        final ItemType itemTypeTemp = itemType;
        final Integer itemKindCodeTemp = itemKindCode;
        List<FlowItem> flowItems = (List<FlowItem>) jdbcTemplate.query(queryBuildrStr.toString(), new ResultSetExtractor() {
            List<FlowItem> flowItemsInner = new ArrayList<FlowItem>();
            @Override
            public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
                while(resultSet.next()){
                    FlowItem flowItem = new FlowItem();
                    flowItem.setID(resultSet.getInt("ITEM_ID"));
                    flowItem.setName(resultSet.getString("ITEM_NAME"));
                    flowItem.setKindName(resultSet.getString("ITEM_KIND_NAME"));
                    flowItem.setType(itemTypeTemp);
                    flowItem.setKindCode(itemKindCodeTemp);
                    flowItemsInner.add(flowItem);
                }
                return flowItemsInner;
            }
        }, itemType.getVlaue(),itemKindCode);
        return flowItems ;
    }
}
